package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.User;
import team.yidong.fenda.service.HospitalService;
import team.yidong.fenda.service.MasterService;
import team.yidong.fenda.service.UserService;

import javax.jws.soap.SOAPBinding;
import javax.persistence.ManyToMany;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.DoubleSummaryStatistics;

/**
 * Created by xuanlong on 2017/10/23.
 */
@Controller
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    MasterService masterService;

/*    @RequestMapping("/user")
    public String user(Model model, HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = hospitalService.findHospitalById(hospitalId).getSysName();
        model.addAttribute("sysName", sysName);
        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes((new String[]{"hospitalPermissionSet", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList"}));
            model.addAttribute("hospital", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
        } else {
            JSONArray jsonArray = hospitalService.findAll();
            jsonArray.remove(0);
            model.addAttribute("hospital", jsonArray);
        }

        return "user";
    }*/

    @RequestMapping("/findUser")
    @ResponseBody
    public JSONObject findUser(HttpSession session, String hospitalName, Integer limit, Integer offset, HttpServletRequest request, String wxNumber, String name, Double score) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        return userService.findUser(hospitalId, hospitalName, limit, offset, wxNumber, name, score);
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public void addUser(@RequestParam String wxNumber, @RequestParam String name, @RequestParam String info, @RequestParam String hospitalName) {
        userService.addUser(wxNumber, name, info, hospitalName);

    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteHospital(@RequestParam String jsonData) {
        JSONArray jsonArray = JSONArray.fromObject(jsonData);
        Iterable<User> userIterable = JSONArray.toCollection(jsonArray, User.class);
        userService.deleteUser(userIterable);
        return "删除成功";
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateHospital(@RequestParam String jsonData) {
        JSONObject jsonObject = JSONObject.fromObject(jsonData);
        User user = (User) JSONObject.toBean(jsonObject, User.class);
        userService.updateUser(user);
        return "修改成功";
    }


}